﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 在运行中保存的数据
    /// </summary>
    class RuningData
    {
        //Room的缓存
        public static List<RoomModel> Rooms = new List<RoomModel>();
        #region 操纵Rooms的方法
        public static bool RoomsInit()
        {
            if (Rooms.Count == 0)
            {

                string sql = "SELECT * FROM tb_Room";
                SqlDataReader sdr = DatabaseHelper.GetSqlDataReader(sql);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        //封装对象保存在缓存中
                        RoomModel rm = new RoomModel();
                        rm.ID = Convert.ToInt32(sdr["ID"].ToString().Trim());
                        rm.RoomName = sdr["RoomName"].ToString().Trim();
                        rm.RoomJC = sdr["RoomJC"].ToString().Trim();
                        rm.RoomBJF = Convert.ToInt32(sdr["RoomBJF"].ToString().Trim());
                        rm.RoomWZ = sdr["RoomWZ"].ToString().Trim();
                        rm.RoomZT = sdr["RoomZT"].ToString().Trim();
                        rm.RoomType = sdr["RoomType"].ToString().Trim();
                        rm.RoomBZ = sdr["RoomBZ"].ToString().Trim();
                        rm.RoomQT = sdr["RoomQT"].ToString().Trim();
                        rm.GuestName = sdr["GuestName"].ToString().Trim();
                        rm.ZhangDan = sdr["zhangdanDate"].ToString().Trim();
                        rm.Num = Convert.ToInt32(sdr["Num"].ToString().Trim());
                        rm.WaiterName = sdr["WaiterName"].ToString().Trim();
                        RuningData.Rooms.Add(rm);
                    }
                }
                sdr.Close();
                return true;
            }
            return false;
        }

        public static bool AddRoom(RoomModel room)
        {
            foreach (RoomModel item in Rooms)
            {
                if (item.ID == room.ID)
                {
                    //id相同不插入
                    return false;
                }
            }
            Rooms.Add(room);
            return true;
        }

        public static void UpdateRoom(RoomModel room)
        {
            string sql;
            for(int i = 0; i < Rooms.Count; i++)
            {
                //碰到id相同就跟新
                if(Rooms[i].ID == room.ID)
                {
                    Rooms[i] = room;
                    sql = string.Format("UPDATE tb_Room SET RoomName='{0}',RoomJC='{1}',RoomBJF={2},RoomWZ='{3}',RoomZT='{4}',RoomType='{5}',RoomBZ='{6}',RoomQT='{7}',GuestName='{8}',zhangdanDate='{9}',Num={10},WaiterName='{11}' WHERE ID={12}",
                        room.RoomName, room.RoomJC, room.RoomBJF, room.RoomWZ, room.RoomZT, room.RoomType, room.RoomBZ, room.RoomQT, room.GuestName, room.ZhangDan, room.Num, GetWaiter(room.WaiterName).WaiterNum, room.ID);
                    DatabaseHelper.ExecuteUpdate(sql);
                    return;
                }
            }
            //没有相同的就添加
            Rooms.Add(room);
            //todo inset
        }

        public static bool DeleteRoom(RoomModel room)
        {
            foreach(RoomModel item in Rooms)
            {
                if(item.ID == room.ID)
                {
                    Rooms.Remove(room);
                }
            }
            return false;
        }

        public static bool DeleteRoom(int roomId)
        {
            for (int i = 0; i < Rooms.Count; i++)
            {
                if (Rooms[i].ID == roomId)
                {
                    Rooms.RemoveAt(i);
                }
            }
            return false;
        }

        public static RoomModel GetRoom(int roomId)
        {
            foreach(RoomModel item in Rooms)
            {
                if(item.ID == roomId)
                {
                    return item;
                }
            }
            return null;
        }
        #endregion

        //Waiter缓存
        public static List<WaiterModel> Waiters = new List<WaiterModel>();
        #region 操纵Waiters的方法
        public static bool WaitersInit()
        {
            if (Waiters.Count == 0)
            {
                string sql = "SELECT * FROM tb_Waiter";
                SqlDataReader sdr = DatabaseHelper.GetSqlDataReader(sql);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        WaiterModel waiter = new WaiterModel();
                        waiter.ID = Convert.ToInt32(sdr["ID"].ToString().Trim());
                        waiter.WaiterName = sdr["WaiterName"].ToString().Trim();
                        waiter.CardNum = sdr["CardNum"].ToString().Trim();
                        waiter.WaiterNum = sdr["WaiterNum"].ToString().Trim();
                        waiter.Sex = sdr["Sex"].ToString().Trim();
                        waiter.Age = Convert.ToInt32(sdr["Age"].ToString().Trim());
                        waiter.Tel = sdr["Tel"].ToString().Trim();
                        Waiters.Add(waiter);
                    }
                }
                sdr.Close();
                return true;
            }
            return false;
        }

        public static WaiterModel GetWaiter(string waitersName)
        {
            foreach (WaiterModel item in Waiters)
            {
                if (item.WaiterName == waitersName)
                {
                    return item;
                }
            }
            return null;
        }
        #endregion

        //Food缓存
        public static List<FoodModel> Foods = new List<FoodModel>();

        #region 操纵Foods的方法
        public static bool FoodsInit()
        {
            if(Foods.Count == 0)
            {
                string sql = "select * from tb_food";
                SqlDataReader sdr = DatabaseHelper.GetSqlDataReader(sql);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        FoodModel item = new FoodModel();
                        item.ID = Convert.ToInt32(sdr["ID"].ToString().Trim());
                        item.FoodType = sdr["foodty"].ToString().Trim();
                        item.FoodNum = Convert.ToInt32(sdr["foodnum"].ToString().Trim());
                        item.FoodName = sdr["foodname"].ToString().Trim();
                        item.FoodPrice = Convert.ToInt32(sdr["foodprice"].ToString().Trim());
                        Foods.Add(item);
                    }
                }
                sdr.Close();
                return true;
            }
            return false;
        }

        public static FoodModel GetFood(string foodName)
        {
            foreach(FoodModel item in Foods)
            {
                if (item.FoodName.Equals(foodName))
                {
                    return item;
                }
            }
            return null;
        }
        #endregion

        public static List<GuestFoodModel> GuestFoods = new List<GuestFoodModel>();

        #region 操纵GuestFoods的方法
        public static bool GuestFoodsInit()
        {
            if(GuestFoods.Count == 0)
            {
                string sql = "select * from tb_GuestFood";
                SqlDataReader sdr = DatabaseHelper.ExecuteSelect(sql);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        GuestFoodModel item = new GuestFoodModel();
                        item.ID = Convert.ToInt32(sdr["ID"].ToString());
                        item.FoodNum = sdr["foodnum"].ToString().Trim();
                        item.FoodName = sdr["foodname"].ToString().Trim();
                        item.FoodSum = sdr["foodsum"].ToString().Trim();
                        item.FoodAllPrice = Convert.ToDouble(sdr["foodallprice"].ToString().Trim());
                        item.WaiterName = sdr["waitername"].ToString().Trim();
                        item.BeiZhu = sdr["beizhu"].ToString().Trim();
                        item.ZhuoTai = sdr["zhuotai"].ToString().Trim();
                        item.DataTime = Convert.ToDateTime(sdr["datatime"].ToString());
                        GuestFoods.Add(item);
                    }
                }
                sdr.Close();
                return true;
            }
            return false;
        }

        public static void UpdateGuestFoods(GuestFoodModel guestFood)
        {
            for (int i = 0; i < GuestFoods.Count; i++)
            {
                if(GuestFoods[i].ID == guestFood.ID)
                {
                    GuestFoods[i] = guestFood;
                    string sql = string.Format("update foodnum='{0}',foodname='{1}',foodsum='{2}',foodallprice={3},waitername='{4}',beizhu='{5}',zhuotai='{6}',datatime='{7}' where ID={8}",
                        guestFood.FoodNum,guestFood.FoodName,guestFood.FoodSum,guestFood.FoodAllPrice,guestFood.WaiterName,guestFood.BeiZhu,guestFood.ZhuoTai,guestFood.DataTime.ToString(),guestFood.ID);
                    DatabaseHelper.ExecuteUpdate(sql);
                    return;
                }
            }
            AddGuestFood(guestFood);
        }

        public static void AddGuestFood(GuestFoodModel guestFood)
        {
            GuestFoods.Add(guestFood);
            string sql = string.Format("insert into tb_GuestFood(foodnum,foodname,foodsum,foodallprice,waitername,beizhu,zhuotai,datatime) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}')",
                guestFood.FoodNum,guestFood.FoodName,guestFood.FoodSum,guestFood.FoodAllPrice,guestFood.WaiterName,guestFood.BeiZhu,guestFood.ZhuoTai,guestFood.DataTime.ToString());
            //string sql = string.Format("insert into tb_GuestFood(foodnum,foodname,foodsum,foodallprice,waitername,beizhu,zhuotai) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}')",
            //    guestFood.FoodNum, guestFood.FoodName, guestFood.FoodSum, guestFood.FoodAllPrice, guestFood.WaiterName, guestFood.BeiZhu, guestFood.ZhuoTai);
            DatabaseHelper.ExecuteInsert(sql);
        }

        public static bool DeleteGuestFood(string foodName)
        {
            foreach(GuestFoodModel item in GuestFoods)
            {
                if (item.FoodName.Equals(foodName))
                {
                    GuestFoods.Remove(item);
                    //ToDo sql执行
                    return true;
                }
            }
            return false;
        }
        #endregion

        //管理加载的页面
        public static Dictionary<String, Form> FormDir = new Dictionary<string, Form>();

        //Form2界面中选中的房间
        public static string CurrRoomNmae = string.Empty;
    }
}
